package com.zisuyi.uilibrary.module.activty.game.spin


import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.weilele.mvvm.utils.activity.*
import com.zisuyi.game.sdk.net.bean.SmallGameInfoBean
import com.zisuyi.uilibrary.base.BaseSmallGameActivity
import com.zisuyi.uilibrary.databinding.ActivitySpinGameBinding
import com.zisuyi.uilibrary.uitils.SoundUtils
import com.zisuyi.uilibrary.view.BaseSpinView

class SpinGameActivity : BaseSmallGameActivity<ActivitySpinGameBinding>() {
    companion object {
        fun start(activity: AppCompatActivity?) {
            activity?.navigateTo<SpinGameActivity>()
        }
    }

    private var spinDatas: MutableList<Int>? = null
    override fun getGameName(): String {
        return GAME_SPIN
    }

    override fun getGameTimesView(): RecyclerView? {
        return mBinding.rcvGameTimes
    }

    override fun getGameTimeDownView(): TextView? {
        return mBinding.tvTimeDown
    }

    private var gameInfo: SmallGameInfoBean.InfoDTO? = null

    override fun onGameInfo(
        isCoolDown: Boolean,
        enableGame: Boolean,
        info: SmallGameInfoBean.InfoDTO
    ) {
        gameInfo = info
        if (spinDatas.isNullOrEmpty()) {
            spinDatas = info.items
            if (spinDatas.isNullOrEmpty()) {
                spinDatas = mutableListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            }
            mBinding.spinView.setSpinData(spinDatas!!.map { BaseSpinView.SpinBean("$it") })
        }
        if (enableGame) {
            if (!isCoolDown) {
                mBinding.btSpin.visible()
                mBinding.flDouble.invisible()
                mBinding.tvVideoAdHint.invisible()
            } else {
                mBinding.btSpin.invisible()
                mBinding.flDouble.visible()
                mBinding.tvVideoAdHint.visible()
            }
        } else {
            mBinding.flDouble.invisible()
            mBinding.btSpin.invisible()
            mBinding.tvVideoAdHint.invisible()
        }
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.btSpin.invisible()
        mBinding.flDouble.invisible()
        mBinding.tvVideoAdHint.invisible()
        mBinding.btSpin.onClick {
            startGame()
        }
        mBinding.ivBack.onClick {
            onBackPressed()
        }
        mBinding.flDouble.onClick {
            showVideoAd()
        }
    }

    private fun startGame() {
        gameInfo?.let {
            SoundUtils.play(SoundUtils.GameMusic.Spin.bean)
            mBinding.btSpin.invisible()
            val index = spinDatas!!.indexOf(it.reward)
            if (index >= 0) {
                mBinding.spinView.startSpin(index, {
                    showRewardDialog()
                    SoundUtils.stop(SoundUtils.GameMusic.Spin.bean)
                }, mBinding.ivRolling)
            } else {
                "Game Error".toast()
            }
        }
    }
}